package code.example.base.util.concurrent.lock.speed;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class CasSpeed extends SpeedTest  {

    volatile int state = 0;
    AtomicIntegerFieldUpdater stateUpdater = AtomicIntegerFieldUpdater.newUpdater(CasSpeed.class,"state");
    @Override
    public void lockRun(SpeedRunnable speedRunnable) {
        while(!stateUpdater.compareAndSet(this,0,1)){

        }
        long startTime = System.currentTimeMillis();
        speedRunnable.run();
        stateUpdater.compareAndSet(this,1,0);
    }
    public static void main(String args []){
        System.out.println("CasSpeed:");
        new CasSpeed().startTest();
    }

}
